<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      chartInstance: null,
      allData: null, // 服务器返回的数据
      startValue: 0, // 区域缩放的开始值
      endValue: 9,  //区域缩放的终点值
      timeId: null
    };
  },
  created() {
    this.$socket.registerCallBack("rankData", this.getData)
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'rankData',
      chartName: 'rank',
      value: ''
    });
    window.addEventListener("resize", this.sceenAdapter);
    this.sceenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.sceenAdapter);
    clearInterval(this.timeId)
    this.$socket.unRegisterCallBack("rankData")
  },
  methods: {
    //初始化echartInstance对象
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.rank_ref, this.theme);
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20,
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on('mouseover', ()=>{
          clearInterval(this.timeId)
      })
      this.chartInstance.on('mouseout', ()=>{
          this.startInterval()
      })

    },
    // 获取服务器的数据
    async getData(res) {
      this.allData = res;
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      this.startInterval()
    },
    // 更新图表
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      //处理数据
      const provinceArr = this.allData.map((item) => {
        return item.name;
      });
      const valueArr = this.allData.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: {
          data: provinceArr,
        },
        dataZoom: {
            show: false,
            startValue: this.startValue,
            endValue: this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    handleSelect(selectType) {
      this.choiceType = selectType;
      this.updateChart();
      this.showChoice = false;
    },
    //适应屏幕大小
    sceenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      //和分辨率相关的文字大小
      const adapterOption = {
        title: {
            textStyle: {
                fontSize: titleFontSize
            }
        },
        series: [
            {
                barWidth: titleFontSize,
                itemStyle: {
                    barBorderRadius: [titleFontSize/2,titleFontSize/2,0,0]
                }
            }
        ]
      };
      this.chartInstance.setOption(adapterOption);

      this.chartInstance.resize();
    },
    startInterval() {
        if(this.timeId) {
            clearInterval(this.timeId)
        }
        this.timeId = setInterval(() => {
            this.startValue++
            this.endValue++
            if(this.endValue > this.allData.length-1) {
                this.startValue = 0
                this.endValue = 9
            }
            this.updateChart()
        },2000)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // console.log('主题切换');
      this.chartInstance.dispose() //销毁当前的图表
      this.initChart()
      this.sceenAdapter()
      this.updateChart()

    }
  }
};
</script>
<style lang='less' scoped>
</style>